package com.glory.dgcpcpda.activity.recipe;

import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.RecipeInfoEntity;
import com.glory.dgcpcpda.entity.UpdateIPEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: TERecipeDBWBActivity
 * Author: XJH
 * Date: 2020/5/14 11:04
 * Description:
 * History:
 */
public class RecipeFreezeActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvBDID)
    TextView tvBDID;
    @BindView(R.id.tvPackageType)
    TextView tvPackageType;
    @BindView(R.id.tvWrie)
    TextView tvWrie;
    @BindView(R.id.etEqpWorkID)
    FreeUI_ClearEditText etEqpWorkID;
    @BindView(R.id.tvEqpWorkType)
    TextView tvEqpWorkType;
    @BindView(R.id.tvRecipeID)
    TextView tvRecipeID;
    @BindView(R.id.tvRecipeEqpID)
    TextView tvRecipeEqpID;
    @BindView(R.id.tvRecipeVersion)
    TextView tvRecipeVersion;
    @BindView(R.id.tvID)
    TextView tvID;
    @BindView(R.id.tvRecipeType)
    TextView tvRecipeType;
    @BindView(R.id.tvDownLoad)
    TextView tvDownLoad;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etReasonMain)
    LinearLayout etReasonMain;
    @BindView(R.id.tvWaferMaterialDesc)
    TextView tvWaferMaterialDesc;
    @BindView(R.id.tvResin)
    TextView tvResin;
    @BindView(R.id.tvCustomerCode)
    TextView tvCustomerCode;
    @BindView(R.id.tvJmthickness)
    TextView tvJmthickness;
    @BindView(R.id.tvWaferSize)
    TextView tvWaferSize;
    @BindView(R.id.tvIspo)
    TextView tvIspo;
    @BindView(R.id.tvMaterialType)
    TextView tvMaterialType;
    @BindView(R.id.tvModelNo)
    TextView tvModelNo;
    @BindView(R.id.tvIpn)
    TextView tvIpn;
    @BindView(R.id.tvTpn)
    TextView tvTpn;
    @BindView(R.id.tvLineID)
    TextView tvLineID;
    @BindView(R.id.tvCircuit)
    TextView tvCircuit;
    @BindView(R.id.etReason)
    EditText etReason;
    private LotInfoEntity mLotInfoEntity = null;
    private RecipeInfoEntity mRecipeInfoEntity = null;
    private String mProcessName = "";
    private String mPlantName = "";
    private String ProcessName = "";
    private int Freeze ;
    private int ID;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_recipe_freeze;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("Recipe冻结/解冻");
        tvBaseSubmit.setText("查询");

        initEdit(etLot, s -> {

            if (getText(etEqpWorkID).equals("")){
                showToast("设备号不能为空",false);
                return;
            }

            FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                    ("正在查询LOT信息",new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                    }.getType(), getSupportFragmentManager(), RecipeFreezeActivity.this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(RecipeFreezeActivity.this,true,getSupportFragmentManager())){
                        mLotInfoEntity = response.body().getResponse().getBody();
                        tvCircuit.setText(mLotInfoEntity.getPRODUCTMODEL());
                        tvLineID.setText(mLotInfoEntity.getLINEID());//订单号
                        tvIpn.setText(mLotInfoEntity.getLOT().getCUSTOMERPARTID());
                        tvTpn.setText(mLotInfoEntity.getTPNID());
                        tvBDID.setText(mLotInfoEntity.getWIRINGNO());
                        tvPackageType.setText(mLotInfoEntity.getLOT().getMAINMATTYPE());
                        int res=mLotInfoEntity.getWIRESPEC().indexOf("/",mLotInfoEntity.getWIRESPEC().indexOf("/")+1);
                        if(res>0){
                            tvWrie.setText(mLotInfoEntity.getWIRESPEC().substring(0,res).toUpperCase());
                        }
                        else{
                            tvWrie.setText(mLotInfoEntity.getWIRESPEC().toUpperCase());
                        }
                        tvResin.setText(mLotInfoEntity.getLOT().getUDF().getRESIN());
                        tvCustomerCode.setText(mLotInfoEntity.getLOT().getCUSTOMERCODE().length() == 4?
                                "GD"+mLotInfoEntity.getLOT().getCUSTOMERCODE():
                                mLotInfoEntity.getLOT().getCUSTOMERCODE());
                        tvTitle.setText("Recipe冻结/解冻" + "(" + mLotInfoEntity.getLOT().getSTEPDESC() + ")");
                        if (ProcessName.equals("DS")){
                            tvWaferMaterialDesc.setText(mLotInfoEntity.getCHIPNAME());
                        }else {
                            tvWaferMaterialDesc.setText(mLotInfoEntity.getWAFERMATERIALDESC());
                        }
                        tvMaterialType.setText(mLotInfoEntity.getYANMO());
                        tvModelNo.setText(mLotInfoEntity.getHUAPIAN());
                        tvIspo.setText(mLotInfoEntity.getISPO());
                        tvJmthickness.setText(mLotInfoEntity.getJMTHICKNESS());
                        tvWaferSize.setText(mLotInfoEntity.getWAFERSIZE());
                    }

                }

                @Override
                public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onError(response);

                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE","LOTINFOBYID");
            map.addProperty("LOTID",s);
            map.addProperty("LOTALIAS","");
            map.addProperty("DURABLE","");
            map.addProperty("EQUIPMENTID",getText(etEqpWorkID));
            NetUtils.requestPostNet("CPC.LOTQUERY","LOTQUERY", RecipeFreezeActivity.this,map,callBack);
        });

//        initEdit(etEqpWorkID, s -> LotUtils.searchEqpInfo(s, TERecipeDBWBActivity.this, getSupportFragmentManager(), new LotUtils.netCallback<EqpInfoEntity>() {
//            @Override
//            public void onSuccess(EqpInfoEntity response) {
//                tvEqpWorkType.setText(response.getEQUIPMENT().getEQPTYPE());
//                mEqpInfoEntity = response;
//            }
//
//            @Override
//            public void onFail(Throwable t) {
//                loadError(t,"EQPQUERY");
//            }
//        }));

        initEdit(etEqpWorkID, new editTextListener() {
            @Override
            public void onTextChangeListener(String s) {
                FreeUI_EntityCallBack<BaseResponseBean<UpdateIPEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<UpdateIPEntity>>
                        (new TypeToken<BaseResponseBean<UpdateIPEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<UpdateIPEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            UpdateIPEntity body = response.body().getResponse().getBody();
                            tvEqpWorkType.setText(body.getPROGRAM_TYPE());
                            mProcessName = body.getPROCESSNAME();
                            mPlantName = body.getFAB();
                            ProcessName = body.getPROCESSNAME();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<UpdateIPEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "GETEQPINFOBYEQPID");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("EQPID",s);
                NetUtils.requestPostNet("EAP.GETEQPINFOBYEQPID","GETEQPINFOBYEQPID", RecipeFreezeActivity.this,map,callBack);

            }
        });
    }

    @Override
    protected void initData() {
    }

    @OnClick({R.id.tvDownLoad, R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvDownLoad:
                if (mRecipeInfoEntity == null){
                    showToast("未获取到Recipe信息",false);
                    return;
                }
                if(getText(etReason).equals("")&&Freeze==0){
                    showToast("冻结原因不能为空！",false);
                    return;
                }

                FreeUI_EntityCallBack<BaseResponseBean<UpdateIPEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<UpdateIPEntity>>
                        (new TypeToken<BaseResponseBean<UpdateIPEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<UpdateIPEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            UpdateIPEntity body = response.body().getResponse().getBody();
                            //返回成功，直接提示
                            if(Freeze==1){
                                showConfirmDialog(RecipeFreezeActivity.this,"解冻成功！");
                                return;
                            }else {
                                showConfirmDialog(RecipeFreezeActivity.this,"冻结成功！");
                                return;
                            }
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<UpdateIPEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "GETEQPINFOBYEQPID");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("id",mRecipeInfoEntity.getId());
                map.addProperty("reason",getText(etReason));
                NetUtils.requestPostNet("EAP.SwitchRecipeFreezeStatus","SwitchRecipeFreezeStatus", this,map,callBack);

                break;
            case R.id.tvBaseSubmit:
                if (getText(etEqpWorkID).equals("")){
                    showToast("设备号不能为空",false);
                    return;
                }

                if (mLotInfoEntity == null){
                    showToast("批次信息不能为空",false);
                    return;
                }
                if (mLotInfoEntity.getLOT().getPROCEDURENAME().equals("DA")) {
                    if (tvWaferMaterialDesc.getText().toString().equals("")) {
                        showToast("芯片名不存在", false);
                        return;
                    }
                }


                LotUtils.searchRecipeInfo(this, getSupportFragmentManager(), mLotInfoEntity, tvWaferMaterialDesc.getText().toString(),getText(etEqpWorkID), new LotUtils.netCallback<RecipeInfoEntity>() {
                    @Override
                    public void onSuccess(RecipeInfoEntity response) {
                        Freeze=response.getFREEZE();
                        if(response.getFREEZE()==1){
                            tvDownLoad.setText("解冻");
                            etReasonMain.setVisibility(View.GONE);
                        }else{
                            tvDownLoad.setText("冻结");
                            etReasonMain.setVisibility(View.VISIBLE);
                        }
                        tvRecipeEqpID.setText(response.getRecipeEqpID());
                        tvRecipeID.setText(response.getRecipeID());
                        tvRecipeType.setText(response.getRecipeType());
                        tvRecipeVersion.setText(response.getRecipeVer());
                        tvID.setText(String.valueOf(response.getId()));
                        mRecipeInfoEntity = response;
                    }

                    @Override
                    public void onFail(Throwable t) {
                        loadError(t,"RECIPEQUERY");
                    }
                },mProcessName,tvEqpWorkType.getText().toString(),mPlantName,tvResin.getText().toString(),tvCustomerCode.getText().toString());
                break;
        }
    }

}
